//
//  ZJGTableViewController.m
//  FangXinLang
//
//  Created by 朱健刚 on 16/9/12.
//  Copyright © 2016年 AJY. All rights reserved.
//

#import "ZJGTableViewController.h"
#import "ZJGTableViewCell.h"
#import <AFNetworking.h>
#import "ZJGview.h"
#import <MBProgressHUD.h>
#import "ZJGdiyigeTableViewCell.h"
#import "HotViewController.h"
#import "ZJGtiaozhuangViewController.h"

@interface ZJGTableViewController ()<ZJGDaiLi>
@property(nonatomic,strong)NSMutableArray *arrM;
@property(nonatomic,strong)UIView *view2;
@property(nonatomic,strong)UIImageView *img1;
@property(nonatomic,assign)CGFloat a;
@end

@implementation ZJGTableViewController
-(NSMutableArray *)arrM{
    if (!_arrM) {
        _arrM=[NSMutableArray array];
    }
    return _arrM;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.contentInset=UIEdgeInsetsMake(-64, 0, 0, 0);
    HotViewController *hot=[[HotViewController alloc]init];
    hot.delegate=self;
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    [manager GET:@"http://newsapi.sina.cn/?resource=rank/list&deviceId=b23f584540e993d0&from=6053295012&weiboUid=&weiboSuid=&imei=863472025667673&wm=b207&chwm=14010_0001&oldChwm=14010_0001&osVersion=5.0.2&connectionType=2&resolution=720x1280&city=CHXX0122&deviceModel=Xiaomi__Xiaomi__MI+2SC&location=38.048464%2C114.483239&link=&mac=f8%3Aa4%3A5f%3A5f%3A35%3Ab1&ua=Xiaomi-MI+2SC__sinanews__5.3.2__android__5.0.2&urlSign=e2b97566b8&rand=154" parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject) {
            NSMutableDictionary *di1=responseObject;
            NSMutableDictionary *di2=di1[@"data"];
            NSArray *arr=di2[@"list"];
            for (NSDictionary *dict in arr) {
                ZJGview *model=[ZJGview ZJGviewWithDict:dict];
                [_arrM addObject:model];
            }
            [self.tableView reloadData];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    self.tableView.showsVerticalScrollIndicator=NO;

}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.arrM.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell=nil;
    if (indexPath.row==0){
        static NSString *ID1=@"cell1";
        ZJGview *model=self.arrM[indexPath.row];
       ZJGdiyigeTableViewCell *cell1=[tableView dequeueReusableCellWithIdentifier:ID1];
        if (cell1==nil) {
            cell1=[ZJGdiyigeTableViewCell ZJGdiyigeTableViewCell];
        }
        cell1.model=model;
        
        cell=cell1;
    }else{
         static NSString *ID=@"cell";
        ZJGview *model=self.arrM[indexPath.row];
        ZJGTableViewCell *cell2=[tableView dequeueReusableCellWithIdentifier:ID];
        if (cell2==nil) {
            cell2=[ZJGTableViewCell ZJGTableViewCell];
        }
        cell2.model=model;
        if ((indexPath.row+1)<10) {
            cell2.img3.image=[UIImage imageNamed:[NSString stringWithFormat:@"hot_rankNum%ld_ico",(long)indexPath.row+1]];
        }

        if ((indexPath.row+1)==10) {
            cell2.img2.image=[UIImage imageNamed:@"hot_rankNum1_ico"];
            cell2.img3.image=[UIImage imageNamed:@"hot_rankNum0_ico"];
        }
       
        cell=cell2;
    }

    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


        ZJGview *model=self.arrM[indexPath.row];
        NSURL *url=[NSURL URLWithString:model.link];
    ZJGtiaozhuangViewController *tiao=[[ZJGtiaozhuangViewController alloc]init];
    //[[UIApplication sharedApplication]openURL:url];
    //[UIApplication sharedApplication].keyWindow.rootViewController=tiao;
    [self.tabBarController presentViewController:tiao animated:YES completion:nil];
    NSURLRequest *request=[NSURLRequest requestWithURL:url];
    
    [tiao.webview loadRequest:request];
    

    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        return 200;
    }else{
        return 150;
    }
}
#pragma mark 监听滚动
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //CGFloat h =self.tableView.contentSize.height+self.tabBarController.tabBar.frame.size.height-[UIScreen mainScreen].bounds.size.height;
   CGFloat y =self.tableView.contentOffset.y-64;
   // NSLog(@"%f",y);


    if (_a>y) {
        _img1.hidden=NO;
        [UIView animateWithDuration:2.5 animations:^{
            _img1.transform=CGAffineTransformRotate(_img1.transform, M_2_PI);
        }];
    }

    if (_a<y) {
        _img1.hidden=NO;
        [UIView animateWithDuration:2.5 animations:^{
            _img1.transform=CGAffineTransformRotate(_img1.transform, -M_2_PI);
        }];
    }
    if (y>0) {
        self.view2.alpha=y/1000;
        //        self.navigationController.navigationBar.alpha=y/1000;
//    self.navigationController.navigationBar.hidden=NO;

    }
    if (y<0) {
        self.view2.alpha=0;
    }
    if (y==0) {

        _img1.hidden=YES;
}
    _a=y;
}
#pragma mark 代理方法
-(void)addColor:(UIView *)view1 andimg:(UIImageView *)img{
    NSLog(@"wertyui");
    _view2=view1;
    _img1=img;
}

@end
